<template>
<div class="col-md-12">
  <my-navbar>
    <template slot="right">  
      <button class="btn btn-default" @click.prevent="onRefresh">
        <span class="glyphicon glyphicon-refresh"></span> {{$t('common.refresh')}}
      </button>           
    </template> 
  </my-navbar>
  <div class="col-md-12">
    <my-table :cfg="cfg" :list="orderList" :loading="loading"></my-table>
  </div>  
</div>
</template>

<script>
import _ from 'lodash'
import toast from 'libs/toast.js'
import msgbox from 'libs/msgbox.js'
import locales from '../locales/log.js'
import {storageApi} from 'share/server/api.js'

export default {
  props: {
    logname: {
      type: String,
      required: true
    },
    cfg: {
      type: Object,
      required: true
    }
  },
  locales,
  data () {
    return {
      list: [],
      loading: false
    }
  },
  computed: {
    orderList () {
      return _.orderBy(this.list, ['date'], ['desc'])
    }
  },
  methods: {
    makeLogs (txt) {
      //console.log(txt)
      var lines = txt.split('\r\n')
      var logs = []
      _.forEach(lines, function(item) {
        var arr = item.split(' : ')
        if (arr && arr.length === 2) {
          var contents = arr[1].split('|')
          if (contents && contents.length >= 7) {
            var log = {
              date: arr[0].slice(0, 19),
              ip: contents[0],
              username: contents[1],
              role: '',
              op: '',
              action: '',
              result: '',
              code: ''
            }
            if (contents[2] !== '') {
              log.role = this.$t(`role.${contents[2]}`)
            }
            if (contents[3] !== '') {
              log.op = this.$t(`common.${contents[3]}`)
            }
            var params
            if (contents[5] !== '') {
              params = JSON.parse(contents[5])
            }
            if (params) {
              log.action = this.$t(`action.${contents[4]}`, params)
            } else {
              log.action = this.$t(`action.${contents[4]}`)
            }
            if (contents[6] !== '') {
              log.result = this.$t(`common.${contents[6]}`)
            }
            let code = parseInt(contents[7])
            if (code > 0) {
              var hex = `h${code.toString(16)}`
              log.code = this.$t(`code.${hex}`)
            } else if (code === 0) {
              log.code = ''
            } else {
              log.code = this.$t('error.code', {code})
            }            
            logs.splice(logs.length, 0, log)
          }
        }
      }.bind(this))
      
      return logs
    },
    onRefresh () {
      this.doGetLogFile()
    },
    onExport () {
    },
    doGetLogFile () {
      storageApi.getFile('log', this.logname).then(function(res) {
        if (res && res.data) {
          this.logs = res.data
          this.list = this.makeLogs(this.logs)
        }
      }.bind(this))
    },
  },
  ready () {
    this.doGetLogFile()
  }
}
</script>

<style>
</style>